var app = getApp();
function checkorder(orderid,that){
	app.util.request({
        url: 'entry/wxapp/checkorder',
        data: {
            m: 'junsion_challeng',
            orderid:orderid
        },
        cachetime: 0,
        success: function (res) {
        	  var data = res.data;
        	  if(data == 1){
    		  that.setData({
    	            openChance: !that.data.openChance
    	      });
              wx.showModal({
                title:'支付成功',
                showCancel:false,
                success: function(res) {
                	that.onLoad();
                  }
              });
            }else{
              setTimeout(function(){
                checkorder(orderid);
              },1000);
            }
         },
    })
}
Page({
	onShareAppMessage:function(){
    
	  var that = this;
	  var mem = that.data.mem;
	  var mid = mem.id;
	  var sharedata = that.data.shareData;
	  sharedata.path = 'challeng/pages/index/index?mid='+mid+'?gType='+that.data.gametype;
	  return {
		  title: sharedata.title,
	     // desc: sharedata.desc,
	      path: sharedata.path,
	      imageUrl:sharedata.pic,
	      success(res){
          var shareTicket = res.shareTickets[0] // 获取 shareTicket
          wx.getShareInfo({
            shareTicket: shareTicket,
            complete(res) {
              //console.log(res) // 输出加密后的 openGId 信息
              app.util.request({
                url: 'entry/wxapp/addnums',
                data: {
                  m: 'junsion_challeng',
                  t: 1,
                  mid: that.data.openid,
                  iv: res.iv,
                  encryptedData: res.encryptedData,
                },
                success: function (res) {
	    	        	that.setData({
	    	                openZhanji: false
	    	            })
	    	        	if(res.data.code == 1){
	    	        		var challeng_num = parseInt(res.data.num);
	    	        		var mem = that.data.mem;
	    	        		mem.challeng_num = challeng_num;
	    	        		that.setData({ mem: mem,openChance:false});
	    	        		wx.showToast({
                        		title:'次数\t+'+res.data.num,
                        		icon:'none',
                				mask:true,
                				duration:1000
                			});
	    	        	}
	    	        	else{
	    	        		 wx.showModal({title:res.data.msg,showCancel:false});
	    	        	}
	    	        }
	    	    });     
	         }
          });
        },
	      fail: function(res) {
	        // 转发失败
	    	  console.log(res);
	      }
	  }
  },
    data:{
    	mem:{},
    	price:{},
    	openid:'',
    	share_num:0,
    	get_share_num:0,
    	poster_switch:0,
    	poster_tips:'',
    	img:'',
    	tips:'',
    	user_tip:'',
    	showShare:false,
    	custom:'',
    	share2:'',
    	u_bg:'',
    	adv:{},
    	kefu_height:100,
    	gametype:0,
    },
    onShow:function(options){
    	var that = this;
    	wx.showLoading({title:'数据加载中...'});
    	var gametype = 0;
    	var sysType = wx.getStorageSync('gameType');
    	if(parseInt(sysType)>=0){
    		gametype = sysType;
    		that.setData({gametype: gametype});
    	}
    	
    	app.sysInit(this,function(res){
    	app.getUserInfo(function (res) {
        	var openid = res['openid'];
        	wx.onUserCaptureScreen(function(res) {
          	    console.log('用户截屏了')
          	    app.util.request({
      	            url: 'entry/wxapp/BlackMem',
      	            data: {
      	              m: 'junsion_challeng',
      	              openid:openid,
      	              t: 1,
      	            },
      	            success: function (res) {
      	                wx.navigateBack({});
      	            },
      	        });
          	})
        	that.setData({openid:openid});
        	app.util.request({
                url: 'entry/wxapp/ismem',
                data: { openid: openid, m: 'junsion_challeng' },
                success: function (res) {
                  if (res.data == 1) {
                    wx.navigateBack({});
                  }
                  else{
                	//获取分享内容
                      app.getShareData(function(res){
                      	that.setData({shareData:res.data});
                      },openid,gametype);
                	  
                      app.util.request({
          		        url: 'entry/wxapp/user',
          		        data: {
          		          m: 'junsion_challeng',
          		          t: 1,
          		          openid:openid,
          		        },
          		        success: function (res) {
          		          wx.hideLoading();
          		          that.setData({
          		        	  price : res.data.price,
          		        	  mem : res.data.mem,
          		        	  get_share_num : res.data.get_share_num,
          		        	  share_num : res.data.share_num,
          		        	  poster_switch : res.data.poster_switch,
          		        	  poster_tips : res.data.poster_tips,
          		        	  custom : res.data.custom,
          		        	  share2 : res.data.share2,
          		        	  u_bg : res.data.u_bg,
          		        	  adv : res.data.adv,
          		        	  user_tip : res.data.user_tip,
          		        	  more_tz : res.data.more_tz,
          		          });
          		          if(res.data.custom){
          		        	  wx.getImageInfo({
          		        		  src: res.data.custom,
          		        		  success: function (res) {
          		        			  var rate = res.width / 100;
          		        			  var height = res.height / rate;
          		        		      that.setData({kefu_height:height});
          		        		  }
          		        	  })
          		          }
          		        },
          		    });
                  }
                },
            });
    	});	
        });	
        wx.setNavigationBarTitle({ title: '用户中心' });
    },
    createPoster:function(){
    	var that = this;
    	wx.showLoading();
		app.util.request({
            url: 'entry/wxapp/Poster',
            data: {
                m : 'junsion_challeng',
                t : 1,
                openid:that.data.openid,
            },
            success: function (res) {
              wx.hideLoading();
              console.log(res);
              that.setData({
	          	  openZhanji: false
	          })
              if(res.data.code == 1){
            	  //生成海报成功
            	  that.setData({showShare:true,img:res.data.img,tips:res.data.tips});
              }else{
            	  wx.showModal({title:res.data.msg,showCancel:false});
              }
            },
        });
    },
    
    saveImgToPhotosAlbumTap:function(){
  	  var that = this;
  	  var img = that.data.img;
  		wx.downloadFile({  
  		  url: img,  
  		  success:function(res){  
  		    wx.saveImageToPhotosAlbum({  
  		      filePath: res.tempFilePath,  
  		      success: function (res) {  
	  		      wx.showToast({
	              	  title: '保存海报成功',
	              })
	              that.setData({showShare:false});
  		      },  
  		      fail: function (res) {
  		        wx.showModal({title:'保存海报失败',showCancel:false});
  		        that.setData({showShare:false});
  		      }  
  		    })  
  		  },  
  		  fail:function(res){  
  		    console.log('下载海报失败')  
  		  }  
  		});
    },
    goThere:function(e){
    	var that = this;
    	var dataset = e.currentTarget.dataset;
    	var index = dataset.index;
    	var adv = that.data.adv;
    	adv = adv[index];
    	if(adv.appid){
    		wx.navigateToMiniProgram({
    	        appId: adv.appid,
    	        path: adv.link,
        });
    	    return;
    	}
    	else if(adv.link.indexOf('http') >= 0){
    		wx.setStorageSync('advurl', adv.link);
    	    wx.navigateTo({
    	        url: '../web/web',
    	    })
    	}else{
        wx.reLaunch({
          url: adv.link,
        })
      }
    },
    goBuy:function(e){
    	var that = this;
    	var dataset = e.currentTarget.dataset;
    	var selkey = dataset.index;
    	var paytype = 1;
		wx.showLoading();
		app.util.request({
            url: 'entry/wxapp/CreateOrder',
            data: {
                m : 'junsion_challeng',
                t : 1,
                openid:that.data.openid,
                paytype:paytype,
                selkey:selkey,
            },
            success: function (res) {
              wx.hideLoading();
              if(res.data.code == 1){
            	  res = res.data.res;
            	  res['success'] = function(){
            		  checkorder(res.orderid,that)
                  };
                  wx.requestPayment(res);
              }else{
            	  wx.showModal({title:res.data.msg,showCancel:false});
              }
            },
        });
    },
    //获得更多机会
    showChance: function (e) {
        this.setData({
            openChance: !this.data.openChance
        })
    },
    //炫耀战绩
    showZhanji:function(e){
        this.setData({
            openZhanji: !this.data.openZhanji
        })
    }
})
